home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3447 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: Sun, 28 Jan 1996 22:54:04 GMT
  6. Organization: Netcom
  7. Message-ID: <310bfdbc.170966400@nntp.ix.netcom.com>
  8. References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net> <4e6hse$dvl@ns.RezoNet.NET> <310a2389.49571776@nntp.ix.netcom.com> <4eg6h8$1db5@info.estec.esa.nl>
  9. NNTP-Posting-Host: ix-dc16-21.ix.netcom.com
  10. X-NETCOM-Date: Sun Jan 28  2:53:38 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. 328pt@SPOD2.dev.esoc.esa.de (Phil Tregoning) wrote:
  14.  
  15. > >> In referenced article, John R Buchan says...
  16. > >>
  17. > >> >>   cpy = (char *) malloc(MAXLEN);
  18. > >> >
  19. > >> >The cast is unnecessary and can hide errors.  You should remove it.
  20. > > 
  21. > >ray@ultimate-tech.com (Ray Dunn) wrote:
  22. > >
  23. > >> [...] adding a cast to a pointer of the same 
  24. > >> type to the malloc return is the *safest* thing you can do:
  25. > >> 
  26. > >>   fred = malloc(n * sizeof(int));
  27. > >> 
  28. > >> Oops - fred isn't an "int *" it's a "long *", but the compiler wont 
  29. > >> issue any warnings, but in:
  30. > >> 
  31. > >>   fred = (int *)malloc(n * sizeof(int));
  32. > >> 
  33. > >> the compiler will issue an error.
  34. > miker3@ix.netcom.com (Mike Rubenstein) replied :
  35. > >Why will the compiler issue an error in that case?  It's completely
  36. > >legal code.
  37. > From K&R2 section 5.4 :
  38. >   "It is not legal to [stuff about illegal pointer arithmatic], or even,
  39. >    except for void *, to assign a pointer of one type to a pointer of
  40. >    another type without a cast."
  41. > If you assign a int * to a long * the compiler can certainly pick that up.
  42.  
  43. Whoops.  You are correct.  In the famous words of Emily Latella,
  44. "never mind."
  45.  
  46.  
  47. Michael M Rubenstein
  48.